﻿SearchGiaBDSDuAn = function () {
    searchClick = function (w) {
        var tt = $('#txtStateID').val();
        var ten = "";
        if ($('#txtKw').val() != "Tên dự án" && $('#txtKw').val() != "")
            ten = $('#txtKw').val();
        var da = $('#txtLoaiBds').val();
        var pag = "giaduan.aspx";
        if (w == "1") pag = "widgets/wdggda.aspx";
        else {
            // chua login
            if (nvgUtils.getCookie('GAgentID') == null || nvgUtils.getCookie('GAgentID') == "") {
                LoginRequest('bang-gia-bat-dong-san/');
                return;
            }
        }

        window.location.href = pathClient + pag + "?p=1&tt=" + tt + "&qh=" + $('#txtSuburb').val() + "&da=" + da + "&kw=" + ten;
    };
    bindData = function () {
        //$('#'+$('#hdfCtrlGiaDuAn').val()+'_sltDa').val($('#hdfDuan').val());
        if ($('#hdfKw').val() != "")
            $('#txtKw').val($('#hdfKw').val());
        if ($('#txtStateID').val() != "") {
            showTextTopSelect('hrfState', 'iptState', $('#txtStateText').val());
            showQuanHuyen($('#txtStateID').val());
        }
        if ($('#txtSuburb').val() != "") {
            showTextTopSelect('hrfSuburb', 'iptSuburb', $('#txtSuburbText').val());
        }
        if ($('#txtLoaiBds').val() != "") {
            showTextTopSelect('hrfLoaiBDS', 'iptLoaiBDS', $('#txtLoaiBdsText').val());
        }
    };
    showTinhThanh = function () {
        var url = pathClientAjax + "services/formsearchhandler.asmx/LoadStateRegionRfsF";
        $.post(url, {},
                function (data) {
                    taoStateList(data.lst, "divState", "txtStateID");
                }, 'json');
    };
    showQuanHuyen = function (st) {
        $('#txtStateID').val(st);
        var url = pathClientAjax + "services/formsearchhandler.asmx/LoadSuburb";
        $.post(url, { st: st },
		        function (data) {
		            taoList(data.lst, 'divSuburb', 'txtSuburb', "suburb");
		        }
	        , 'json');
    };
    taoList = function (lll, divid, hdf, key_) {
        $('#' + divid).html("");
        if (lll != "" && lll != null) {
            var tpl__ = "";
            if (key_ == "suburb") {
                tpl__ = $.template("<li><a href=\"javascript:;\" onclick=\"$('#txtSuburb').val('${eValue}');showTextTopSelect('hrfSuburb','iptSuburb','${eName}');$('#divListSuburb').hide();\"> ${eName}</a></li>");
                $('#' + divid).append("<li><a href=\"javascript:;\" onclick=\"$('#txtSuburb').val('');showTextTopSelect('hrfSuburb','iptSuburb','');$('#divListSuburb').hide();\">Chọn tất cả</a></li>");
            }
            for (var i = 0; i < lll.length; i++) {
                var eID = lll[i].s1;
                var eName = lll[i].s2;
                $('#' + divid).append(tpl__, { eValue: eID, eName: eName, eHdf: hdf });
            }
        }
    };
    taoStateList = function (lll, divid, hdf) {
        // $('#' + divid).html("");
        if (lll != null && lll != "") {
            var tpl__ = "";
            var tplS__ = "";
            tpl__ = $.template("<li><a href=\"javascript:;\" onclick=\"javascript:SearchGiaBDSDuAn.showQuanHuyen('${eValue}','${eName}');showTextTopSelect('hrfState','iptState','${eName}');$('#divDropState').hide();\">${eName}</a></li>");
            tplS__ = $.template("<li><a href=\"javascript:;\" onclick=\"javascript:SearchGiaBDSDuAn.showQuanHuyen('${eValue}','${eName}');showTextTopSelect('hrfState','iptState','${eName}');$('#divDropState').hide();\"><span>${eName}</span></a></li>");
            $('#divStateN').append("<li class=\"header\">MIỀN BẮC</li>");
            $('#divStateM').append("<li class=\"header\">MIỀN TRUNG</li>");
            $('#divStateS').append("<li class=\"header\">MIỀN NAM</li>");
            for (var i = 0; i < lll.length; i++) {
                var eID = lll[i].s1;
                var eName = lll[i].s2;
                var eRegion = lll[i].s3;
                if (eRegion == "1") {

                    if (nvgUtils.CheckSplitValue(eID, BigProvinces, ",")) {
                        $('#divStateN').append(tplS__, { eValue: eID, eName: eName, eHdf: hdf });
                    }
                    else {
                        $('#divStateN').append(tpl__, { eValue: eID, eName: eName, eHdf: hdf });
                    }
                }
                if (eRegion == "2") {
                    if (nvgUtils.CheckSplitValue(eID, BigProvinces, ",")) {
                        $('#divStateM').append(tplS__, { eValue: eID, eName: eName, eHdf: hdf });
                    }
                    else {
                        $('#divStateM').append(tpl__, { eValue: eID, eName: eName, eHdf: hdf });
                    }
                }
                if (eRegion == "3") {
                    if (nvgUtils.CheckSplitValue(eID, BigProvinces, ",")) {
                        $('#divStateS').append(tplS__, { eValue: eID, eName: eName, eHdf: hdf });
                    }
                    else {
                        $('#divStateS').append(tpl__, { eValue: eID, eName: eName, eHdf: hdf });
                    }
                }
            }
        }
    };
    showTextTopSelect = function (hrf, ipt, con) {
        if (con == "") {
            $('#' + ipt).hide();
            $('#' + hrf).show();
            $('#' + ipt).val("");
        }
        else {
            $('#' + hrf).hide();
            $('#' + ipt).show();
            $('#' + ipt).val(con);
        }
    };
    CheckLoadQuanHuyen = function () {
        if ($('#txtStateID').val() != "") {
            $('.hiddenDropBox').hide();
            $('#divListSuburb').toggle();
        } else {
            alert("Bạn phải chọn tỉnh/thành phố!");
            return false;
        }
    };
    ActiveTab = function (tab, classShow) {
        $('.marketPage ul li').removeClass("current");
        $(tab).parent("h4").parent("li").addClass("current");
        $('.marketPage .contentSearch').hide();
        $(classShow).show();
    };
    return { searchClick: searchClick, bindData: bindData, showTinhThanh: showTinhThanh, taoStateList: taoStateList, showTextTopSelect: showTextTopSelect, showQuanHuyen: showQuanHuyen
            , CheckLoadQuanHuyen: CheckLoadQuanHuyen, ActiveTab: ActiveTab
    };
} ();
SearchGiaBDSDuAn.showTinhThanh();